home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / misc / math / YACAS.lha / yacas / bin / yacas_client < prev   
Text File  |  2000-12-05  |  4KB  |  135 lines

  1. #!/bin/sh
  2.  
  3. # command_client: barebones notebook interface under Unix
  4. # Written by Serge Winitzki.
  5. #
  6. # Requirements: sh or bash, mkfifo, cat, tee, /dev/null
  7. #    Usage:
  8. #        command_client "application" [options] < inputfile
  9. #    Note: quotes are needed around the application command if it contains spaces. Customize the variables "quit" and "extracommand" for your application.
  10. #    On first call, a fifo pipe is created in /tmp and the target application is started in the background and set up to continuously get input from the fifo ("server mode"). On subsequent calls, input from STDIN is passed to the application. Any input given on the first call is also passed to the application. 
  11. #    The fifo pipe is named according to the username and application name. Existence of the pipe is taken as a sign that the "server" is up and running. By definition, only one session can be run by one user on one machine.
  12. #    The pipe should be removed when the target application quits. This can be done by "trap" function which is a bash built-in but it may not be available under other shells.
  13. #    Option "-stop" explicitly removes the pipe and stops the server:
  14. #        command_client "application -flags" -stop
  15. #    Option "-pass" copies input to the ooutput
  16. #        command_client "application -flags" -pass
  17.  
  18. ############################################################
  19. #
  20. # Determine the command to launch interactive application
  21. #
  22.  
  23. if [ `basename $0` = yacas_client ] ; then
  24.     command="yacas"
  25.     quit="quit"    # Command to quit application
  26.     # Special setup for Yacas
  27.     extracommand="cat -v | sed -e 's/^.*^\[\[K^M//g; s/\^M^\[\[[0-9]*C.*$//'"
  28. else
  29.     command="$1"    # Run application
  30.     extracommand="cat"    # Command to prettify application output
  31.     quit="quit"    # Command to quit application
  32.     if [ "X$1" = X ] ; then
  33.         echo "command_client : barebones notebook interface for Unix"
  34.         echo "Usage: `basename $0` command [-stop] [-pass] < input"
  35.         exit
  36.     else
  37.         shift
  38.     fi
  39. fi
  40.  
  41.  
  42. #
  43. # Make up a name of the fifo pipes based on username and command
  44. # Note that "$fifo" may contain spaces!
  45. #
  46.  
  47. fifo="/tmp/fifo.$USER.$command"
  48.  
  49. #
  50. # Auxiliary files
  51. #
  52.  
  53. curout="$fifo.tmp"
  54. infifo="$fifo.in"
  55.  
  56. cleanup() {
  57.     rm -f "$infifo" "$curout"
  58. #    echo "command_client: exiting... ($1)" # Debugging
  59. }
  60.  
  61. #
  62. # Check if -restart option is given
  63. #
  64.  
  65. if [ "X$1" = "X-stop" ] ; then
  66.     [ -f "$curout" ] && cat "$curout" | eval "$extracommand"
  67. #    echo "$quit" >> $infifo
  68. #    sleep 2
  69.     cleanup "stopping"
  70.     echo "Server stopped."
  71.     exit
  72. fi
  73.  
  74. #
  75. # Check if -pass option is given
  76. #
  77.  
  78. if [ "X$1" = "X-pass" ] ; then
  79.     pass=yes
  80.     shift
  81. else
  82.     pass=no
  83. fi
  84.  
  85. #
  86. # Check whether we are being run the first time, i.e. whether the fifo pipe exists.
  87. #
  88.  
  89. if [ ! -p "$infifo" ] ; then    # This is our first run
  90.     echo "Starting server."
  91.     # Fifo does not exist, so we have to create it
  92.     cleanup "first run"
  93.     mkfifo "$infifo" #"$outfifo"
  94.     cat /dev/null > "$curout"
  95.     # Start the "server"
  96. #    trap cleanup 0    # traps aren't working well for us
  97. #    (
  98.         (
  99. #        ("$command" < "$infifo">> "$curout") &    # This doesn't work b/c of repeated opening of pipe
  100.         while test -p "$infifo"; do
  101.             sleep 1    # Reduce system load
  102.             cat
  103.         done
  104.         echo "$quit"
  105.         sleep 1
  106.         ) < "$infifo" | ( $command; cleanup "quit" ) >> "$curout" &
  107. #    ) &
  108.     sleep 1
  109. fi
  110.  
  111.  
  112. #
  113. # Pass our input to the "server" and optionally pass through to output
  114. #
  115.  
  116. if [ "$pass" = "yes" ] ; then
  117.     tee -a "$infifo"
  118. else
  119.     cat >> "$infifo"
  120. fi
  121.  
  122. #
  123. # Print parts of the output that aren't yet printed
  124. #
  125.  
  126. sleep 2    # Allow some time for processing
  127. if test -f "$curout"; then
  128.     cat "$curout" | eval "$extracommand"
  129.     cat /dev/null > "$curout"
  130. fi
  131.  
  132. #
  133. # End
  134. #
  135.